package com.mezz.bones.framework.web.config;

import com.mezz.bones.framework.web.filter.ApiDisableFilter;
import com.mezz.bones.framework.web.filter.ApmHttpInfoFilter;
import com.mezz.bones.framework.web.filter.MdcFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: zhuang
 * @Date: 2024/7/12 17:44
 * @Description: TODO
 * @Version: 1.0
 **/

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    //过滤器排序 值越小优先级越高
    public static final int API_DISABLE_FILTER_ORDER = 9000;
    public static final int MDC_FILTER_ORDER = 10000;
    public static final int APM_FILTER_ORDER = 10010;



    @Resource
    private MdcFilter mdcFilter;

    @Resource
    private ApiDisableFilter apiDisableFilter;

    @Resource
    private ApmHttpInfoFilter apmHttpInfoFilter;

    @Bean
    public FilterRegistrationBean<MdcFilter> mdcFilterRegistrationBean() {
        FilterRegistrationBean<MdcFilter> registrationBean = new FilterRegistrationBean<>(mdcFilter);
        registrationBean.addUrlPatterns("/*"); // 设置过滤的 URL 模式
        registrationBean.setOrder(MDC_FILTER_ORDER); // 设置过滤器的执行顺序
        return registrationBean;
    }

    @Bean
    public FilterRegistrationBean<ApiDisableFilter> apiDisableFilterRegistrationBean() {
        FilterRegistrationBean<ApiDisableFilter> registrationBean = new FilterRegistrationBean<>(apiDisableFilter);
        registrationBean.addUrlPatterns("/*"); // 设置过滤的 URL 模式
        registrationBean.setOrder(API_DISABLE_FILTER_ORDER); // 设置过滤器的执行顺序
        return registrationBean;
    }

    @Bean
    public FilterRegistrationBean<ApmHttpInfoFilter> apmHttpInfoFilterRegistrationBean() {
        FilterRegistrationBean<ApmHttpInfoFilter> registrationBean = new FilterRegistrationBean<>(apmHttpInfoFilter);
        registrationBean.addUrlPatterns("/*"); // 设置过滤的 URL 模式
        registrationBean.setOrder(APM_FILTER_ORDER); // 设置过滤器的执行顺序
        return registrationBean;
    }
}
